﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Boomerang.Management.UIForms;
using Boomerang.Common.Crm;
using Boomerang.Common.Enumerations;
using Boomerang.Common.Crm.DiscoveryService;
using Boomerang.Management.UIControlers;

namespace Boomerang.Management.UIControls
{
    public partial class CrmLogonControl : UserControl
    {

        public CrmLogonControl()
        {
            InitializeComponent();
            this.comboBoxProtocol.SelectedIndex = 0;
        }

        public CrmLogonControl(CrmConnectionOptions options)
            : this()
        {
            Options = options;
        }


        public CrmConnectionOptions Options
        {
            get
            {
                CrmConnectionOptions options = new CrmConnectionOptions();
                options.AuthMode = radioButtonHosted.Checked ? CrmConnectionType.IFD : CrmConnectionType.OnPremise;
                options.DiscoveryServerUrl = textBoxServer.Text;
                if (checkBoxDefaultCredentials.Checked)
                {
                    options.UseDefaultCredentials = true;
                }
                else
                {
                    options.UseDefaultCredentials = false;
                    options.Password = textBoxPassword.Text;
                    options.UserName = textBoxUserName.Text;
                    options.Domain = textBoxDomain.Text;
                }

                options.CrmService = textBoxCrmService.Text;
                options.MetadataService = textBoxMetadataService.Text;

                options.OrgName = comboBoxOrg.Text;
                options.UseHttps = (comboBoxProtocol.SelectedIndex==1);

                return options;
            }

            set
            {

                radioButtonHosted.Checked = (value.AuthMode == CrmConnectionType.IFD) ? true : false;

                textBoxServer.Text = value.DiscoveryServerUrl;

                if (value.UseDefaultCredentials)
                {
                    checkBoxDefaultCredentials.Checked = true;
                }
                else
                {
                    checkBoxDefaultCredentials.Checked = false;
                    textBoxPassword.Text = value.Password;
                    textBoxUserName.Text = value.UserName;
                    textBoxDomain.Text = value.Domain;
                }

                textBoxCrmService.Text = value.CrmService;
                textBoxMetadataService.Text = value.MetadataService;

                comboBoxOrg.Text = value.OrgName;
                comboBoxProtocol.SelectedIndex = (value.UseHttps) ? 1 : 0;

            }
        }

        private void radioButtonOnPremise_CheckedChanged(object sender, EventArgs e)
        {
            radioButtonHosted.Checked = !radioButtonOnPremise.Checked;
        }

        private void radioButtonHosted_CheckedChanged(object sender, EventArgs e)
        {
            radioButtonOnPremise.Checked = !radioButtonHosted.Checked;
        }

        private void checkBoxDefaultCredentials_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxCredentials.Enabled = !checkBoxDefaultCredentials.Checked;
        }

        private void buttonRetrieveOrg_Click(object sender, EventArgs e)
        {
            try
            {
                comboBoxOrg.Items.Clear();

                List<OrganizationDetail> orgs = CrmConnection.GetOrganizations(this.Options);                

                foreach (OrganizationDetail item in orgs)
                {
                    comboBoxOrg.Items.Add(new ComboBoxItem<OrganizationDetail>(item,item.OrganizationName));
                }

            }
            catch (Exception ex)
            {
                ExceptionBox.Show(this.ParentForm,ex,"An error ocurred retrieving the organizations");                
            }
        }

        private void comboBoxOrg_SelectedIndexChanged(object sender, EventArgs e)
        {
            OrganizationDetail org = ((ComboBoxItem<OrganizationDetail>)comboBoxOrg.SelectedItem).Item;

            textBoxCrmService.Text = org.CrmServiceUrl;
            textBoxMetadataService.Text = org.CrmMetadataServiceUrl;
        }

    }
}
